//
//  YTLoadingView.m
//  YiTuUserClient
//
//  Created by 石少庸 on 2017/3/21.
//  Copyright © 2017年 YiTu. All rights reserved.
//

#import "YTLoadingView.h"
#import "YTPopupView.h"
#import <DGActivityIndicatorView/DGActivityIndicatorView.h>

@interface YTLoadingView ()

@property (strong, nonatomic) YTPopupView *popupView;

@end

@implementation YTLoadingView

+ (instancetype)sharedInstance {
    static YTLoadingView *loading = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        loading = [[self alloc] init];
    });
    
    return loading;
}

#pragma mark - show

+ (void)show {
    [[self sharedInstance] loadingView:YTLodingViewAnimationTypeRotatingTrigons color:kMainBlueColor];
}

+ (void)showLoadingViewWithType:(YTLodingViewAnimationType)type {
    [[self sharedInstance] loadingView:type color:kMainBlueColor];
}

+ (void)showLoadingViewWithType:(YTLodingViewAnimationType)type
                      tintColor:(UIColor *)color {
    [[self sharedInstance] loadingView:type color:color];
}

#pragma mark - dismiss

+ (void)dismissCompletion:(void (^)())completion {
    [[self sharedInstance] dismissWithCompletion:completion];
}

+ (void)dismiss {
    [[self sharedInstance] dismissWithCompletion:nil];
}

#pragma mark - private

- (void)dismissWithCompletion:(void(^)())completion {
    [_popupView dismiss:YES];
    [_popupView removeFromSuperview];
    _popupView.didFinishDismissingCompletion = ^() {
        if (completion) {
            completion();
        }
    };
}

- (void)loadingView:(YTLodingViewAnimationType)type
              color:(UIColor *)color {
    DGActivityIndicatorView *loadingView = [[DGActivityIndicatorView alloc] initWithType:(DGActivityIndicatorAnimationType)type tintColor:color size:50.f];
    loadingView.frame = CGRectMake(0, 0, 50, 50);
    
//    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
//    blurView.frame = CGRectMake(0, 0, 80, 80);
//    blurView.layer.cornerRadius = 5.0;
//    [blurView.contentView addSubview:loadingView];
    
    _popupView = [YTPopupView popupWithContentView:loadingView];
    _popupView.shouldDismissOnContentTouch = NO;
    _popupView.shouldDismissOnBackgroundTouch = NO;
    _popupView.maskType = YTPopupMaskTypeClear;
    [_popupView show];
    [loadingView startAnimating];
}

@end
